from PySide2.QtWidgets import QSpacerItem, QSizePolicy, QPushButton, QHBoxLayout
from pandas import DataFrame

from common.config import File_Temp, Public_Data
from api.api_project import download_file_to_save, get_contract_df,get_cols_values_dict,get_col_values
from common.pyside2_table.qt_wgt import ButtonWithLink
from common.pyside2_table.qtable import Base_Table



# 添加下载控件
def add_download_widget(layout: QHBoxLayout, table: Base_Table, tb_name):
    """添加文函下载控件"""
    right_spacer = QSpacerItem(50, 20, QSizePolicy.Expanding, QSizePolicy.Minimum)
    btn_download_file = QPushButton("下载所选")
    btn_open_dir = ButtonWithLink("打开文件夹", File_Temp)
    layout.addItem(right_spacer)
    layout.addWidget(btn_download_file)
    layout.addWidget(btn_open_dir)

    btn_download_file.clicked.connect(lambda: download_files(table, tb_name))


# 下载文函
def download_files(table: Base_Table, tb_name):
    select_row_df: DataFrame = table.get_selected_rows()
    if select_row_df.empty:
        return
    once_id_list = select_row_df["once_id"].to_list()
    for once_id in once_id_list:
        download_file_to_save(File_Temp, tb_name, once_id)


def get_public_contract_info():
    """根据合同信息，获取相关数据"""
    data = Public_Data()
    df: DataFrame = get_contract_df()
    data.Df_Contract = df
    # 合同信息选择列表
    data.Project_list = get_col_values(df, "project", "所有")
    data.Factory_list = get_col_values(df, "lot_code", "所有")
    data.Lot_list = get_col_values(df, "factory", "所有")

    # 获取合同：项目-作者代码-采购包号-传单的号-合同id的信息
    contract_col_list = ["id", "project", "lot_code", "channel_code", "author_code"]
    data.contract_info_dict = get_cols_values_dict(df, contract_col_list)




